package com.spark.lola.controller;


import com.alibaba.fastjson.JSON;
import com.spark.lola.entity.warehouse.StoreInfo;
import com.spark.lola.mapper.ProductInfoMapper;
import com.spark.lola.service.IProductInfoService;
import com.spark.lola.support.warehouse.configuration.WarehouseKafkaTopic;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.entity.warehouse.ProductInfo;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyejun
 * @since 2020-06-10
 */
@RestController
@RequestMapping("/api/product-info")
public class ProductInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProductInfoController.class);
    @Autowired
    private ProductInfoMapper currentMapper;
    @Autowired
    private IProductInfoService currentService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @RequestMapping(value = "map")
    public ResponseBean map(@RequestParam("key")String key) {
        return ResponseBean.success(currentService.selectMap(key));
    }

    @RequiresPermissions(value = "m7120")
    @RequestMapping(value = "/findBy", consumes = "application/json")
    public ResponseBean findBy(@RequestBody QueryParam<ProductInfo> queryBean,
                               @SessionAttribute ShiroUser shiroUser) {
        PageParam<ProductInfo> pageParam = queryBean.toPageParam();
        IPage<ProductInfo> page = currentService.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }

    @RequiresPermissions(value = "m7124")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute ShiroUser shiroUser) {
        ProductInfo model;
        if (id == 0) {
            model = new ProductInfo();
        } else {
            model = currentService.getById(id);
        }
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = {"m7121", "m7123"}, logical = Logical.OR)
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody ProductInfo form, @SessionAttribute ShiroUser shiroUser) throws Exception {
        ProductInfo model = new ProductInfo();
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.getById(form.getId());
        }
        DataBeanUtils.buildUpdateBean(model, form);
        currentService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = "m7125")
    @RequestMapping("/on")
    public ResponseBean on(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.Y, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m7126")
    @RequestMapping("/un")
    public ResponseBean un(@RequestParam String nos) {
        int count = currentService.updateUsedByIds(CommonConstant.N, nos);
        return ResponseBean.success(null, count);
    }

    @RequiresPermissions(value = "m7122")
    @RequestMapping("/del")
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        ids.forEach(id -> {
            currentService.removeById(id);
        });
        return ResponseBean.success();
    }

    @RequiresPermissions(value = "m7121")
    @RequestMapping(value = "/sync", method = RequestMethod.GET)
    @Transactional
    public ResponseBean sync(@SessionAttribute ShiroUser shiroUser) {
        kafkaTemplate.send(WarehouseKafkaTopic.SYNC_PRODUCT,"syn-product");
        return ResponseBean.success();
    }
}
